// 数据接口 只需更改myColumns数据，其他无需更改        myColumns['columns']：X轴数据标签  myColumns['data']：Y轴数据

function dataColumns() {
    myColumns = {};
    myColumns['columns'] = ['0','20', '40', '60', '80','100', '120', '140', '160'];
    myColumns['data'] = [1, 0.875, 0.75, 0.625,0.5, 0.375, 0.25, 0.125, 0];   // 默认显示数据一
    tex = ["线性标签"];
    // 按钮控件
    var arr = document.getElementsByTagName('button');
    console.log(arr)
    for (var i = 0; i < arr.length; i++) {
        arr[i].onclick = function () {
            if (this.id == 'data_one') {
                myColumns['data'] = [1, 0.875, 0.75, 0.625,0.5, 0.375, 0.25, 0.125, 0];   // 数据一
                tex = ["线性标签"];
                draw(myColumns,tex);
                console.log(1)
            } else if (this.id == 'data_two') {
                myColumns['data'] = [0.85, 0.85, 0.85, 0.85,0.85, 0.6375, 0.425, 0.2125, 0];   // 数据二
                tex = ["分段式标签"];
                draw(myColumns,tex);
                console.log(2)
            } else if (this.id == 'yuce') {
                // if (document.getElementById("canshu1").value == "" || document.getElementById("canshu2").value== ""){
                //     alert( "未完整设置模型参数" );
                // }else if (document.getElementsByName("city")[0].value == ""){
                if (document.getElementsByName("city")[0].value == ""){
                    alert( "未选择需要预测的设备" );
                }else{
                    myFunction();
                    console.log(555);
                }                            
            }
            else if (this.id == 'chuancan') {
                if (document.getElementsByName("city")[0].value == ""){
                    alert( "未选择需要预测的设备" );
                }else if(document.getElementById("canshu1").value == "" || document.getElementById("canshu2").value== ""){
                    alert( "请完整设置模型参数" );         
                }    
                else{
                    MakeForm();
                }          
            }
        };
    }
    console.log(tex);
    return myColumns, tex;
}

// myColumns = dataColumns()[0];
// tex = dataColumns()[1];

function draw(myColumns,tex) {

    let myChart = echarts.init(document.getElementById('new_line'),'dark');
    myChart.clear();

    // 指定图表的配置项和数据
    option = {
        title: {
            text: tex,
            //subtext: 'Example in MetricsGraphics.js',
            left: 'center',
            padding: 15,
            textStyle: { //主标题文本样式{"fontSize": 18,"fontWeight": "bolder","color": "#333"}
				//                 fontFamily: 'Arial, Verdana, sans...',
                fontSize: 22,
                fontStyle: 'normal',
//                 fontWeight: 'normal',
                color: '#fcfcfcc4' 
            },
        },
        tooltip: {
            trigger: 'axis'
        },
        xAxis: {
            type: 'category',
            name: '时间/h',
            nameLocation:'end',
            data: myColumns.columns,
            axisLabel: {
                color: '#fcfcfcc4'
            },
        },
        yAxis: {
            type: 'value',
            name: '健康指标',
            nameLocation:'end',
            axisLabel: {
                color: '#fcfcfcc4'
            },
        },
        series: [{
            data: myColumns.data,
            type: 'line',
            symbol: 'none',  //取消折点圆圈
            color: "cyan",
        }],
    };

    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option, true);
}

function myFunction(){
    document.getElementById('tag1').value = 4.1345;
    document.getElementById('tag2').value = 1.2288;
    document.getElementById('tag3').value = 0.1345;
    var equipSelect = document.getElementsByName("city")[0].value;
    // var index=equipSelect.selectedIndex ; // selectedIndex代表的是你所选中项的index
    // var equipSelect_value1 = equipSelect.options[index].text;
    // console.log(equipSelect_value1)
    console.log(equipSelect)

    document.getElementById("shichang").value = 300;
}

function MakeForm(){  
    var value1 = document.getElementById("canshu1").value;
    console.log(value1)
    var value2 = document.getElementById("canshu2").value;
    console.log(value2)
    $.ajax({
        type : "post",
        url : "Shopping/servlet/BuyServlet",//处理的servlet的url
        data : {
            "canshu1":value1, //要传到后端的数据，是json格式
            "canshu2":value2,
        },
        async: false,
        dataType : "json",
        success : function(data) {
            // 如果传送成功，后端会返回data
        },
        error : function(error) {
            console.log("传参失败")
        }
    });
}


// var arr1 = document.getElementsByTagName('button');
// for (var i = 0; i < arr1.length; i++) {
//     arr1[i].onclick = function () {
//         if (this.id == 'yuce') {
//             myFunction();
//         }
//         else if (this.id == 'chuancan') {
//             MakeForm();
//         }
//     };
// };